package cn.edu.sjtu.stap.cfg;

import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ASTNode;
import cn.edu.sjtu.stap.graph.GraphNode;

public abstract class CfgNode extends GraphNode {
    private static long nodeCount = 0;

    protected CfgNodeKind kind;

    protected ASTNode astNode;

    // protected String content;

    protected String sourceLocation;

    public CfgNode(CfgNodeKind kind) {
        super(++nodeCount);
        setKind(kind);
    }

    public CfgNodeKind getKind() {
        return kind;
    }

    public void setKind(CfgNodeKind kind) {
        if (null == kind) {
            throw new NullPointerException("Node kind should not be null!");
        }
        this.kind = kind;
    }

    public ASTNode getASTNode() {
        return astNode;
    }

    public void setASTNode(ASTNode astNode) {
        this.astNode = astNode;
        makeSourceLocation();
    }

    public String getName() {
        return kind.getName();
    }

    public String getLocationInfo() {
        return sourceLocation;
    }

    private void makeSourceLocation() {
        if (astNode == null) {
            return;
        }
        sourceLocation = astNode.sourceStart() + "|" + astNode.sourceEnd();
    }

    public String toString() {
        StringBuffer s = new StringBuffer();
        s.append("(Node Id: ");
        s.append(this.getId());
        s.append(", Source Location: ");
        s.append(this.getLocationInfo());
        s.append(", Name: ");
        s.append(this.getName());
        s.append(", Content: ");
        s.append(astNode == null ? null : astNode.toString());
        // s.append(content);
        s.append(")");

        return s.toString();
    }
}
